package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.MyAskAndAnswerAdapter;
import com.ihighest.app.infox.adapter.MyMassageAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 我的消息
 * 
 * @author lkx Created at 2015-11-6 上午10:01:53
 * @version 1.0
 */
public class MyMassageActivity extends BaseActivity {
	private ActivityTitle title;
	private PullToRefreshListView massageListview;
	private MyMassageAdapter adapter;
	private String titleName;
	private int page = 1;// 请求数据的页数，默认为1
	private JSONArray array;
	private JSONArray moreArray;
	private long time;
	private TextView onData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_mycollection);

		Const.UNREADMESSAGE = 0;
		titleName = getIntent().getStringExtra("title");

		initView();

	}

	/**
	 * 初始化数据
	 * 
	 * 
	 * @author lkx Created at 2015-11-4 上午11:56:07
	 */
	private void initData() {
		ApiParam param = ApiParam.create();
		param.addParam("page", page);
		if (page >= 2) {
			param.addParam("time", time);
		}
		Api.get("me/myMessage", param, new ApiCallback(MyMassageActivity.this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					if (page == 1) {
						// 无数据显示，隐藏listview
						onData.setVisibility(View.VISIBLE);
						massageListview.setVisibility(View.GONE);
					} else {
						// 页数减1
						page = page > 1 ? --page : 1;
						onData.setVisibility(View.GONE);
						massageListview.setVisibility(View.VISIBLE);
					}

				} else {
					onData.setVisibility(View.GONE);
					massageListview.setVisibility(View.VISIBLE);
					if (page == 1) {
						array = result.getDatas();
						adapter.setData(array);
					} else {
						moreArray = result.getDatas();
						try {
							array = AppKit.joinJSONArray(array, moreArray);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						adapter.appendData(moreArray);
					}
					time = result.getTime();
				}
				massageListview.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				// TODO Auto-generated method stub
				massageListview.onRefreshComplete();
			}
		}, MyMassageActivity.this);

	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-13 上午10:12:02
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.mActivityTitle);
		title.setTitle(titleName);
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleMsg, title.titleRefresh, title.rightLayout);
		massageListview = (PullToRefreshListView) findViewById(R.id.minelistView);
		onData = (TextView) findViewById(R.id.noCollection);
		adapter = new MyMassageAdapter(MyMassageActivity.this, array);
		onData.setVisibility(View.GONE);
		massageListview.setVisibility(View.VISIBLE);
		massageListview.setAdapter(adapter);
		massageListview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				page = 1;
				initData();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				page++;
				initData();
			}

		});
		massageListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View item, int position, long id) {

				Intent intent = new Intent(MyMassageActivity.this, MassageDetailActivity.class);
				try {
					intent.putExtra("massageId", array.getJSONObject(position - 1).getString("id"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		initData();
		super.onResume();
	}

}
